package com.blockmeta.bbs.immodule.widget;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


/**
 * @author AirThor
 * @date 2019/2/28.
 */
public class IMRecyclerView extends RecyclerView implements View.OnLayoutChangeListener {
    int index;

    private OnListTouched mOnListTouched;
    private boolean move;

    public void setOnListTouched(OnListTouched onListTouched) {
        mOnListTouched = onListTouched;
    }

    public interface OnListTouched {
        void onListTouched();
    }

    public IMRecyclerView(Context context) {
        super(context);
        initialize();
    }

    public IMRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public IMRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    private void initialize() {
        addOnLayoutChangeListener(this);
        addOnScrollListener(mOnScrollListener);
    }

    @Override
    public void setAdapter(Adapter adapter) {
        super.setAdapter(adapter);
//        adapter.registerAdapterDataObserver(new IMDataObserver());
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if (bottom < oldBottom) {
            Adapter adapter = getAdapter();
            if (adapter != null) {
                int position = adapter.getItemCount() - 1;
                if (position > 0) {
                    smoothScrollToPosition(position);
                }
            }
        }
    }



    OnScrollListener mOnScrollListener = new OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            if (move){
                move = false;
                //获取要置顶的项在当前屏幕的位置，mIndex是记录的要置顶项在RecyclerView中的位置
                LinearLayoutManager manager = (LinearLayoutManager) getLayoutManager();
                int n = index - manager.findFirstVisibleItemPosition();
                if ( 0 <= n && n < recyclerView.getChildCount()){
                    //获取要置顶的项顶部离RecyclerView顶部的距离
                    int top = recyclerView.getChildAt(n).getTop();
                    //最后的移动
                    recyclerView.smoothScrollBy(0, top);
                }
            }
        }
    };

    public void scollToPositionNow(int n) {
        scrollToPosition(n);
    }

    public void scollToPosition(int n) {
        if(n < 0){
            return;
        }
        LinearLayoutManager manager = (LinearLayoutManager) getLayoutManager();
        //滑动到指定的item
        this.index = n ;
        //拿到当前屏幕可见的第一个position跟最后一个postion
        int firstItem = manager.findFirstVisibleItemPosition();
        int lastItem = manager.findLastVisibleItemPosition();
        //区分情况
        if (n <= firstItem ){
            //当要置顶的项在当前显示的第一个项的前面时
            smoothScrollToPosition(n);
        }else if ( n <= lastItem ){
            //当要置顶的项已经在屏幕上显示时
            int top = getChildAt(n - firstItem).getTop();
            smoothScrollBy(0,top);
        }else{
            //当要置顶的项在当前显示的最后一项的后面时
            smoothScrollToPosition(n);
            //这里这个变量是用在RecyclerView滚动监听里面的
            move = true;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        if (mOnListTouched != null) {
            mOnListTouched.onListTouched();
        }
        return super.onInterceptTouchEvent(e);
    }



    //    /**
//     * 观察者在数据增加并且数据超出一屏幕时自动滚动到底部
//     */
//    class IMDataObserver extends RecyclerView.AdapterDataObserver {
//        public IMDataObserver() {
//
//        }
//
//        @Override
//        public void onChanged() {
//            AutoScroll();
//        }
//
//        @Override
//        public void onItemRangeChanged(int positionStart, int itemCount) {
//            AutoScroll();
//        }
//
//        @Override
//        public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
//            AutoScroll();
//        }
//
//        @Override
//        public void onItemRangeInserted(int positionStart, int itemCount) {
//            AutoScroll();
//        }
//
//        @Override
//        public void onItemRangeRemoved(int positionStart, int itemCount) {
//
//        }
//
//        @Override
//        public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
//
//        }
//    }
//
//    private void AutoScroll() {
//        int itemCount = getLayoutManager().getItemCount();
//        if (itemCount > 0 && canScroll) {
//            //第一次滑动不做平滑滚动 消除视觉上闪动的问题
//            if (smooth) {
//                smoothScrollToPosition(itemCount - 1);
//            } else {
//                scrollToPosition(itemCount - 1);
//                smooth = true;
//            }
//        }
//    }
//
//
//    /**
//     * 监听聊天列表的滚动状态，如果滚动后最后可见项不是最后一个数据，那么将关闭自滚动
//     */
//    OnScrollListener mOnScrollListener = new OnScrollListener() {
//        @Override
//        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
//            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
//                int lastVisiblePosition = ((LinearLayoutManager) getLayoutManager()).findLastCompletelyVisibleItemPosition();
//                int lastItemPosition = getLayoutManager().getItemCount() - 1;
//                if (lastItemPosition > lastVisiblePosition) {
//                    canScroll = false;
//                } else {
//                    canScroll = true;
//                }
//            }else {
//                canScroll = false;
//            }
//        }
//
//        @Override
//        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//
//        }
//    };
}
